package com.sxt.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * redis消息队列配置-订阅者
 * @author Administrator
 *
 */
@Configuration
public class RedisConfig {
	/**
     * 	创建连接工厂
     * @param connectionFactory
     * @param listenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅多个频道,配置订阅项
        container.addMessageListener(listenerAdapter(),new PatternTopic("phone"));
        container.addMessageListener(listenerAdapterTest2(),new PatternTopic("chat"));
        return container;
    }

    /**
     * 	绑定消息监听者和接收监听的方法
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(){
    	/**
		 * 	通过反射机制
		 * 1.  参数1表示监听类
		 * 2. 参数2表示方法名
		 */
		return new MessageListenerAdapter(new RedisReceiver(), "receiveMessage");
    }
    /**
     * 	绑定消息监听者和接收监听的方法
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapterTest2(){
        return new MessageListenerAdapter(new RedisReceiver(),"receiveMessage2");
    }
   

}
